package com.fyb.basic.demo;

import java.util.*;

/**
 * @Author：MR.FANG
 * @name：ArrayListDemo
 * @Date：2025/7/2 下午3:12
 */
public class ArrayListDemo {

    public static void main(String[] args) {
//        remove();
//        testArrays();
//        reverse();
    }

    private static void remove(){
        List<String> list = new ArrayList<>();
        for (int i = 1; i <= 10; ++i) {
            list.add(i + "");
        }
        // 并发加锁
        Iterator<String> iterator = list.iterator();
        while (iterator.hasNext()) {
            String next = iterator.next();
            if ("1".equals(next)) {
                iterator.remove();
            }
        }
        System.out.println(list);
    }

    private static void reverse() {
        String[] s = new String[]{
                "dog", "lazy", "a", "over", "jumps", "fox", "brown", "quick", "A"
        };
        List<String> list = Arrays.asList(s);
        Collections.reverse(list);
        s = list.toArray(new String[0]);//没有指定类型的话会报错
        System.out.println(s);
    }

    /**
     * 手动将数组转为list
     *
     * @param array
     * @param <T>   表示该方法是泛型方法 List<T>：返回值的类型
     * @return
     */
    public static <T> List<T> arrayToList(final T array[]) {
        final List<T> list = new ArrayList(array.length);
        for (T t : array) {
            list.add(t);
        }
        return list;
    }

    private static void testArrays() {
        String[] myArray = {"Apple", "Banana", "Orange"};
        List<String> myList = Arrays.asList(myArray);
        //上面两个语句等价于下面一条语句 将字符串数组转换为list
        List<String> list = Arrays.asList("Apple", "Banana", "Orange");
        list.add("geng"); // Exception in thread "main" java.lang.UnsupportedOperationException
        System.out.println(list);
    }
}
